package com.systop.party08.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.systop.party08.R;
import com.systop.party08.base.BaseFragment;
import com.systop.party08.entity.ArticleListEntity;
import com.systop.party08.http.MyStringCallback;
import com.systop.party08.utils.SPUtils;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

import static com.systop.party08.http.ComUrl.ARTICLE;
import static com.systop.party08.utils.StaticDateUtils.PAGE;
import static com.systop.party08.utils.StaticDateUtils.SP_TOKEN;

/**
 *
 */
public class HomeArticleFragment extends BaseFragment {

    @BindView(R.id.home_article_lv)
    ListView lv;
    @BindView(R.id.home_article_refresh)
    SmartRefreshLayout refresh;
    @BindView(R.id.layout_no_data_tv)
    TextView tvNoData;


    private View view;

    private Unbinder unbinder;
    private CommonAdapter<ArticleListEntity.DataBeanX.DataBean> adapter;
    private List<ArticleListEntity.DataBeanX.DataBean> list = new ArrayList<>();
    private int page = PAGE;
    private boolean isLoad;

    private String id = "";
    public static HomeArticleFragment newInstance(String id) {
        HomeArticleFragment fragment = new HomeArticleFragment();
        Bundle args = new Bundle();
        args.putString("id", id);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            id = getArguments().getString("id");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home_article, container, false);
        unbinder = ButterKnife.bind(this, view);
        initLv();
        refresh.autoRefresh();
        return view;
    }

    private void getData() {
        if (view != null && !isLoad) {
            page = PAGE;
            isLoad = true;
           // okhttp();
        }
    }

    private void initLv() {
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getData();
            }
        });
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
               // okhttp();
            }
        });
        adapter = new CommonAdapter<ArticleListEntity.DataBeanX.DataBean>(getActivity(), R.layout.item_article, list) {
            @Override
            protected void convert(ViewHolder viewHolder, ArticleListEntity.DataBeanX.DataBean item, int position) {
                viewHolder.setText(R.id.item_article_tv_title, item.getTitle());
                viewHolder.setText(R.id.item_article_tv_date, item.getPubDate());
            }
        };
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //新闻详情
            }
        });
    }

    private void okhttp() {
        HashMap<String, String> param = new HashMap<>();
        param.put("catId", id);
        param.put("token", SPUtils.getPreference(getActivity(), SP_TOKEN));
        param.put("pageSize", "10");
        param.put("pageNumber", page + "");
        commonOkhttp.execute(ARTICLE, param, new MyStringCallback(getActivity(), false) {
            @Override
            public void onSuccess(String response) {
                super.onSuccess(response);
                commonOkhttp.refreshComplete(page, refresh);
                isLoad = false;
                ArticleListEntity result = gson.fromJson(response, ArticleListEntity.class);
                setData(result);
            }

            @Override
            public void onOther(String response, int status, String message) {
                super.onOther(response, status, message);
                commonOkhttp.refreshComplete(page, refresh);
                isLoad = false;
            }

            @Override
            public void onError(String error) {
                super.onError(error);
                commonOkhttp.refreshCompleteF(page, refresh);
                isLoad = false;
            }
        });
    }

    private void setData(ArticleListEntity result) {
        if (view == null) return;
        List<ArticleListEntity.DataBeanX.DataBean> dataBeans = result.getData().getData();
        if (page == PAGE) {
            //第一页 刷新
            list.clear();
            if (dataBeans != null && dataBeans.size() > 0) {
                //有数据
                list.addAll(dataBeans);
                ++page;
                if (tvNoData != null) {
                    tvNoData.setVisibility(View.GONE);
                }
                if (refresh != null) {
                    refresh.setEnableLoadMore(true);
                }
            } else {
                if (refresh != null) {
                    refresh.setEnableLoadMore(false);
                    tvNoData.setVisibility(View.VISIBLE);
                }
            }
        } else {
            //加载下一页
            if (dataBeans != null && dataBeans.size() > 0) {
                list.addAll(dataBeans);
                ++page;
            } else {
                showMessage(getString(R.string.list_bottom));
            }
        }
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}